<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */ 
namespace app\mobile\controller;
use think\Db;
class Cart extends MobileBase {
    
    public $cartLogic; // 购物车逻辑操作类    
    public $user_id = 0;
    public $user = array();        
    /**
     * 析构流函数
     */
    public function  __construct() {   
        parent::__construct();                
        $this->cartLogic = new \app\home\logic\CartLogic();
        if(session('?user'))
        {
        	$user = session('user');
                $user = M('users')->where("user_id", $user['user_id'])->find();
                session('user',$user);  //覆盖session 中的 user               			                
        	$this->user = $user;
        	$this->user_id = $user['user_id'];
        	$this->assign('user',$user); //存储用户信息
                // 给用户计算会员价 登录前后不一样
                if($user){
                    $user[discount] = (empty($user[discount])) ? 1 : $user[discount];
                    DB::execute("update `__PREFIX__cart` set member_goods_price = goods_price * {$user[discount]} where (user_id ={$user[user_id]} or session_id = '{$this->session_id}') and prom_type = 0");
                }                 
         }            
    }
    
    public function cart(){
        //判读用户没绑定手机 跳转绑定手机
        if($this->user_id)
        {
            $user_info = M('users')->where("user_id",$this->user_id)->find();
            if($user_info['mobile_validated'] ==0 ){
                $this->redirect('mobile/User/mobile_validate');
                exit();
            }
        }
        //获取热卖商品
        $hot_goods = M('Goods')->where('is_hot=1 and is_on_sale=1')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();
        $this->assign('hot_goods',$hot_goods);
        return $this->fetch('cart');
    }
    /**
     * 将商品加入购物车
     */
    function addCart()
    {
        $goods_id = I("goods_id/d"); // 商品id
        $goods_num = I("goods_num/d");// 商品数量
        $goods_spec = I("goods_spec"); // 商品规格                
        $goods_spec = json_decode($goods_spec,true); //app 端 json 形式传输过来
        $unique_id = I("unique_id"); // 唯一id  类似于 pc 端的session id
        $user_id = I("user_id",0); // 用户id        
        $result = $this->cartLogic->addCart($goods_id, $goods_num, $goods_spec,$unique_id,$user_id); // 将商品加入购物车
        exit(json_encode($result)); 
    }
    
    /**
     * ajax 将商品加入购物车
     */
    function ajaxAddCart()
    {
        $goods_id = I("goods_id/d"); // 商品id
        $goods_num = I("goods_num/d");// 商品数量
        $goods_spec = I("goods_spec/a",array()); // 商品规格             
        
        $type = M('goods')->where('goods_id',$goods_id)->getField('type');
   
        if($type == 1){
	        if(is_array($goods_spec['other_content'])){
	        	if(empty($goods_spec['other_content']['capacity'])) return array('status'=>-1,'msg'=>'请选择购买容量','result'=>'');
	            if(empty($goods_spec['other_content']['buy_type'])) return array('status'=>-1,'msg'=>'请选择购买方式','result'=>'');
	            if(empty($goods_spec['other_content']['buy_type']) == 2){
		            if(empty($goods_spec['other_content']['cellar_type'])) return array('status'=>-1,'msg'=>'请选择窖藏方式','result'=>'');
		            if($goods_spec['other_content']['cellar_type'] == 1){
		            	if(empty($goods_spec['other_content']['yy_time'])) return array('status'=>-1,'msg'=>'请选择到厂时间','result'=>'');
		            	if(empty($goods_spec['other_content']['yy_num'])) return array('status'=>-1,'msg'=>'请选择预约人数','result'=>'');
		            }   
	            }
	        }
        }
        if($type == 2){
        	if(is_array($goods_spec['other_content'])){
        	   if(!empty($goods_spec['other_content']['despic'])){//若有图片就转存图片
        	   	if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $goods_spec['other_content']['despic'], $result)){
				  $type = $result[2];
				  $new_file = "./public/upload/destine/{$this->user_id}_".time().".{$type}";
				  if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $goods_spec['other_content']['despic'])))){
				      $goods_spec['other_content']['despic'] = $new_file;//转存，释放内存
				  }
				}
        	   } 
        	}
        }
        $result = $this->cartLogic->addCart($goods_id, $goods_num, $goods_spec,$this->session_id,$this->user_id); // 将商品加入购物车
        exit(json_encode($result));
    }

    /*
     * 请求获取购物车列表
     */
    public function cartList()
    {
        
        
        $cart_form_data = input('cart_form_data'); // goods_num 购物车商品数量
        $cart_form_data = json_decode($cart_form_data,true); //app 端 json 形式传输过来

        $unique_id = I("unique_id"); // 唯一id  类似于 pc 端的session id
        $user_id = I("user_id/d"); // 用户id
        $where['session_id'] = $unique_id; // 默认按照 $unique_id 查询
        if($user_id){
            $where['user_id'] = $user_id;
          
        }
        $cartList = M('Cart')->where($where)->getField("id,goods_num,selected");

        if($cart_form_data)
        {
            // 修改购物车数量 和勾选状态
            foreach($cart_form_data as $key => $val)
            {
                $data['goods_num'] = $val['goodsNum'];
                $data['selected'] = $val['selected'];
                $cartID = $val['cartID'];
                if(($cartList[$cartID]['goods_num'] != $data['goods_num']) || ($cartList[$cartID]['selected'] != $data['selected']))
                    M('Cart')->where("id", $cartID)->save($data);
            }
            //$this->assign('select_all', $_POST['select_all']); // 全选框
        }

        $result = $this->cartLogic->cartList($this->user, $unique_id,0);
        exit(json_encode($result));
    }
    
    /**
     * 定制-品尝小样下单
     */
    public function sampleOrder()
    {
       if($this->user_id == 0)
            $this->error('请先登陆',U('Mobile/User/login'));
        $address_id = I('address_id/d');
        if($address_id)
            $address = M('user_address')->where("address_id", $address_id)->find();
        else
            $address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
        
        if(empty($address)){
        	header("Location: ".U('Mobile/User/add_address',array('source'=>'cart2')));
        }else{
        	$this->assign('address',$address);
        }

        if($this->cartLogic->cart_count($this->user_id,1) == 0 )
            $this->error ('你的购物车没有选中商品','Cart/cart');

        $result = $this->cartLogic->cartList($this->user, $this->session_id,1,1); // 获取购物车商品
        $shippingList = M('Plugin')->where("`type` = 'shipping' and status = 1")->cache(true,TPSHOP_CACHE_TIME)->select();// 物流公司

        // 找出这个用户的优惠券 没过期的  并且 订单金额达到 condition 优惠券指定标准的
        $sql = "select c1.name,c1.money,c1.condition, c2.* from __PREFIX__coupon as c1 inner join __PREFIX__coupon_list as c2  on c2.cid = c1.id and c1.type in(0,1,2,3) and order_id = 0  where c2.uid = {$this->user_id} and ".time()." < c1.use_end_time and c1.condition <= {$result['total_price']['total_fee']}";
        $couponList = DB::query($sql);
        if(I('cid') != ''){
            $cid = I('cid');
            $checkconpon = M('coupon')->field('id,name,money')->where("id = $cid")->find();    //要使用的优惠券
            $checkconpon['lid'] = I('lid');
        }
        
        $cellar_spec = C('cellar_spec');
        foreach($result['cartList'] as $key=>$val){
        	$type = M("goods")->where("goods_id",$val['goods_id'])->getField('type');
        	if($type == 1){
	        	if(!empty($val['cellar_content'])){
		            $other_content = json_decode($val['cellar_content'],true);
		            foreach($other_content as $k=>$v){
		            	$spec[$key]['capacity'] = $other_content['capacity'];
		            	$spec[$key]['buy_type'] = $other_content['buy_type'];
	            		if($k=='buy_type' && $v==2){
	            			$spec[$key]['cellar_years'] = $other_content['cellar_years'];
	            			$spec[$key]['cellar_type'] = $other_content['cellar_type'];	
	            		}
	            		if($k=='cellar_type' && $v==1 && $spec[$key]['buy_type']==2){
	            			$spec[$key]['yy_time'] = $other_content['yy_time'];
		            	    $spec[$key]['yy_num'] = $other_content['yy_num'];
	            		}
	
		            }
	            }
	            //拼接成字符串在前台直接输出
	            $result['cartList'][$key]['spec_key_name'] = '';
	            $i=0;
	            foreach($spec[$key] as $k=>$v){
	            	$i++;
	         	  	if(is_array($cellar_spec[$k])){
	         	  		$result['cartList'][$key]['spec_key_name'] .= "{$cellar_spec[$k][0]}:{$cellar_spec[$k][$v]}&nbsp;&nbsp;";	
	         	  	}else{	
	         	  	    $result['cartList'][$key]['spec_key_name'] .= "{$cellar_spec[$k]}:{$v}&nbsp;&nbsp;";	
	         	  	}
	         	  	if($i%2==0){
	         	  		$result['cartList'][$key]['spec_key_name'] .= "<br>";
	         	  	}
	         	}
	
	            //以数组方式输出，在前台拼接
	            //$result['cartList'][$key]['spec_key_name'] = $spec[$key];
	        }    
        }
        
        $this->assign('cellar_spec',C('cellar_spec'));//窖藏商品属性名称配置
        $this->assign('couponList', $couponList); // 优惠券列表
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('cartList', $result['cartList']); // 购物车的商品
        $this->assign('total_price', $result['total_price']); // 总计
        $this->assign('checkconpon', $checkconpon); // 使用的优惠券	
    
        return $this->fetch();
    }
    
    
    /**
     * 定制产品订单确认
     */
    public function order()
    {  
       if($this->user_id == 0)
            $this->error('请先登陆',U('Mobile/User/login'));	
       $goods_id = empty(I('goods_id'))?0:I('goods_id'); 	
       $goods_num = empty(I('goods_num'))?0:I('goods_num');
       $goods = M('goods')->alias('a')
                         ->join('destine_goods_attr b','b.goods_id = a.goods_id')
                         ->join('destine_theme c','b.theme_id = c.id')
                         ->join('destine_aroma e','b.aroma_id = e.id')
                         ->join('suppliers f','b.winery_id = f.suppliers_id')
                         ->join('origin g','g.id = f.origin_id')
                         ->join('destmp h','h.id = b.tmp_url')
                         ->field('a.*,b.*,g.title as originname,f.suppliers_address,
                         f.suppliers_name,f.brand_name,f.suppliers_logo,
                         c.title as themename,e.title as aromaname,h.url as tmp_url')
                         ->where('b.goods_id',$goods_id)->find();          
        $address_id = I('address_id/d');
        if($address_id)
            $address = M('user_address')->where("address_id", $address_id)->find();
        else
            $address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
        
        if(empty($address)){
        	header("Location: ".U('Mobile/User/add_address',array('source'=>'cart2')));
        }else{
        	$this->assign('address',$address);
        }
        $shippingList = M('Plugin')->where("`type` = 'shipping' and status = 1")->cache(true,TPSHOP_CACHE_TIME)->select();// 物流公司

        $goods_total_price = $goods['shop_price']*$goods_num;
        
        $this->assign('destine_spec',C('cellar_spec'));//定制商品附属信息
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('goods', $goods); // 当前的定制商品
        $this->assign('goods_num', $goods_num); // 购买数量
        $this->assign('goods_total_price', $goods_total_price); // 总计	
    
        return $this->fetch();
    }
    
    
    /**
     * 购物车第二步确定页面
     */
    public function cart2()
    {
        if($this->user_id == 0)
            $this->error('请先登陆',U('Mobile/User/login'));
        $address_id = I('address_id/d');
        if($address_id)
            $address = M('user_address')->where("address_id", $address_id)->find();
        else
            $address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
        
        if(empty($address)){
        	header("Location: ".U('Mobile/User/add_address',array('source'=>'cart2')));
        }else{
        	$this->assign('address',$address);
        }

        if($this->cartLogic->cart_count($this->user_id,1) == 0 )
            $this->error ('你的购物车没有选中商品','Cart/cart');

        $result = $this->cartLogic->cartList($this->user, $this->session_id,1,1); // 获取购物车商品
        $shippingList = M('Plugin')->where("`type` = 'shipping' and status = 1")->cache(true,TPSHOP_CACHE_TIME)->select();// 物流公司

        // 找出这个用户的优惠券 没过期的  并且 订单金额达到 condition 优惠券指定标准的
        $sql = "select c1.name,c1.money,c1.condition, c2.* from __PREFIX__coupon as c1 inner join __PREFIX__coupon_list as c2  on c2.cid = c1.id and c1.type in(0,1,2,3) and order_id = 0  where c2.uid = {$this->user_id} and ".time()." < c1.use_end_time and c1.condition <= {$result['total_price']['total_fee']}";
        $couponList = DB::query($sql);
        if(I('cid') != ''){
            $cid = I('cid');
            $checkconpon = M('coupon')->field('id,name,money')->where("id = $cid")->find();    //要使用的优惠券
            $checkconpon['lid'] = I('lid');
        }
        
        $freight_free = tpCache('shopping.freight_free');
        $this->assign('freight_free',$freight_free);//全场满包邮
        $this->assign('cellar_spec',C('cellar_spec'));//窖藏商品属性名称配置
        $this->assign('couponList', $couponList); // 优惠券列表
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('cartList', $result['cartList']); // 购物车的商品
        $this->assign('total_price', $result['total_price']); // 总计
        $this->assign('checkconpon', $checkconpon); // 使用的优惠券
        return $this->fetch();
    }

    /**
     * ajax 获取订单商品价格 或者提交 订单
     */
    public function cart3(){

        if($this->user_id == 0)
            exit(json_encode(array('status'=>-100,'msg'=>"登录超时请重新登录!",'result'=>null))); // 返回结果状态
        
        $address_id = I("address_id/d"); //  收货地址id
        $shipping_code =  I("shipping_code"); //  物流编号        
        $invoice_title = I('invoice_title'); // 发票
        $couponTypeSelect =  I("couponTypeSelect"); //  优惠券类型  1 下拉框选择优惠券 2 输入框输入优惠券代码
        $coupon_id =  I("coupon_id/d"); //  优惠券id
        $couponCode =  I("couponCode"); //  优惠券代码
        $pay_points =  I("pay_points",0); //  使用积分
        $user_money =  I("user_money",0); //  使用余额
        $user_note = trim(I('user_note'));   //买家留言
        $user_money = $user_money ? $user_money : 0;
        
        if($this->cartLogic->cart_count($this->user_id,1) == 0 ) exit(json_encode(array('status'=>-2,'msg'=>'你的购物车没有选中商品','result'=>null))); // 返回结果状态
        if(!$address_id) exit(json_encode(array('status'=>-3,'msg'=>'请先填写收货人信息','result'=>null))); // 返回结果状态
        if(!$shipping_code) exit(json_encode(array('status'=>-4,'msg'=>'请选择物流信息','result'=>null))); // 返回结果状态
		
		$address = M('UserAddress')->where("address_id", $address_id)->find();
        $order_goods = M('cart')->where(['user_id'=>$this->user_id,'selected'=>1])->select();	
        foreach($order_goods as $key=>$val){
        	$deduction_amount = M('goods')->where('goods_id',$val['goods_id'])->getField('deduction_amount');
        	$order_goods[$key]['deduction_amount'] = $deduction_amount;
        }
        $result = calculate_price($this->user_id,$order_goods,$shipping_code,0,$address[province],$address[city],$address[district],$pay_points,$user_money,$coupon_id,$couponCode,$type);
                
		if($result['status'] < 0)	
			exit(json_encode($result));      	
	    // 订单满额优惠活动		                
        $order_prom = get_order_promotion($result['result']['order_amount']);
        $result['result']['order_amount'] = $order_prom['order_amount'] ;
        $result['result']['order_prom_id'] = $order_prom['order_prom_id'] ;
        $result['result']['order_prom_amount'] = $order_prom['order_prom_amount'] ;
			
        $car_price = array(
            'postFee'      => $result['result']['shipping_price'], // 物流费
            'couponFee'    => $result['result']['coupon_price'], // 优惠券            
            'balance'      => $result['result']['user_money'], // 使用用户余额
            'pointsFee'    => $result['result']['integral_money'], // 积分支付
            'payables'     => $result['result']['order_amount'], // 应付金额
            'goodsFee'     => $result['result']['goods_price'],// 商品价格
            'order_prom_id' => $result['result']['order_prom_id'], // 订单优惠活动id
            'order_prom_amount' => $result['result']['order_prom_amount'], // 订单优惠活动优惠了多少钱            
        );	

        
        
       
        // 提交订单        
        if($_REQUEST['act'] == 'submit_order')
        {  
        	$userinfo = M('users')->where(array('user_id'=>$this->user_id))->find();
            if($userinfo['is_lock'] == 1){
            	$return_arr = array('status'=>-1,'msg'=>'该账号已被封号，资金冻结，请联系客服！','result'=>''); // 账户被封号，资金冻结，不可下单！
                exit(json_encode($return_arr));
            }
            if(empty($coupon_id) && !empty($couponCode)){
                $coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
            }
            if($buy_type == 1){
            	$result = $this->cartLogic->addOrder($this->user_id,$address_id,$shipping_code,$invoice_title,$coupon_id,$car_price,$user_note,1,$order_goods); // 添加订单
            }
            else{
            	$result = $this->cartLogic->addOrder($this->user_id,$address_id,$shipping_code,$invoice_title,$coupon_id,$car_price,$user_note); // 添加订单
            }
            exit(json_encode($result));
        }
            $return_arr = array('status'=>1,'msg'=>'计算成功','result'=>$car_price); // 返回结果状态
            exit(json_encode($return_arr));
    }
    	
    /*
     * 订单支付页面
     */
    public function cart4(){

        $order_id = I('order_id/d');
        $order = M('Order')->where("order_id", $order_id)->find();
        // 如果已经支付过的订单直接到订单详情页面. 不再进入支付页面
        if($order['pay_status'] == 1){
            $order_detail_url = U("Mobile/User/order_detail",array('id'=>$order_id));
            header("Location: $order_detail_url");
            exit;
        }

        $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and  scene in(0,1)")->select();        
        //微信浏览器,目前微信支付不可用，先注释掉
//      if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
//          $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();            
//      }        
        $paymentList = convert_arr_key($paymentList, 'code');

        foreach($paymentList as $key => $val)
        {
            $val['config_value'] = unserialize($val['config_value']);
            if($val['config_value']['is_bank'] == 2)
            {
                $bankCodeList[$val['code']] = unserialize($val['bank_code']);
            }
            //判断当前浏览器显示支付方式
//          if(($key == 'weixin' && !is_weixin()) || ($key == 'alipayMobile' && is_weixin())){
//              unset($paymentList[$key]);
//          }
            if($key == 'weixin' && !is_weixin()){
                unset($paymentList[$key]);
            }
        }

        $bank_img = include APP_PATH.'home/bank.php'; // 银行对应图片
        $payment = M('Plugin')->where("`type`='payment' and status = 1")->select();
        $this->assign('paymentList',$paymentList);
        $this->assign('bank_img',$bank_img);
        $this->assign('order',$order);
        $this->assign('bankCodeList',$bankCodeList);
        $this->assign('pay_date',date('Y-m-d', strtotime("+1 day")));
        return $this->fetch();
    }
    
    /*
     * 一元夺宝支付页面
     */
    public function indiana_pay(){

        $id = I('id/d');
        $order = M('user_indiana')->where("id", $id)->find();
        // 如果已经支付过的订单直接到订单详情页面. 不再进入支付页面
        if($order['pay_status'] == 1){
        	if($order['is_end'] == 0){
        	    $order_detail_url = U("Mobile/User/indiana_wait",array('id'=>$order_id));	
        	}
        	else{
        		$order_detail_url = U("Mobile/User/indiana_over",array('id'=>$order_id));	
        	}
            header("Location: $order_detail_url");
            exit;
        }

        $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and  scene in(0,1)")->select();        
        //微信浏览器
        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
            $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();            
        }        
        $paymentList = convert_arr_key($paymentList, 'code');

        foreach($paymentList as $key => $val)
        {
            $val['config_value'] = unserialize($val['config_value']);
            if($val['config_value']['is_bank'] == 2)
            {
                $bankCodeList[$val['code']] = unserialize($val['bank_code']);
            }
            //判断当前浏览器显示支付方式
            if(($key == 'weixin' && !is_weixin()) || ($key == 'alipayMobile' && is_weixin())){
                unset($paymentList[$key]);
            }
        }

        $bank_img = include APP_PATH.'home/bank.php'; // 银行对应图片
        $payment = M('Plugin')->where("`type`='payment' and status = 1")->select();
        $this->assign('paymentList',$paymentList);
        $this->assign('bank_img',$bank_img);
        $this->assign('order',$order);
        $this->assign('bankCodeList',$bankCodeList);
        $this->assign('pay_date',date('Y-m-d', strtotime("+1 day")));
        return $this->fetch();
    }
    
    
    /*
     * 一元夺宝支付成功后获取编码
     */
    public function indiana_getcode(){
    	    $id = I('id/d');
    	    $order = M('user_indiana')->where('id',$id)->find();
    	    if($order['pay_status'] == 1){
    	    	$this->success('您已支付成功！正在跳转···',U('Mobile/User/indiana'));
    	    }
    	    $indiana = M('indiana_sale')->where("id",$order['indiana_id'])->find();
    	    if($indiana['stock_num'] < $order['num']){
    	    	//下单失败，退款至付款账户
    			$pay_log = M('pay_log')->where(array('order_sn'=>$order['order_sn']))->find();
    			if($pay_log){
    				$r_data = array('pay_id'=>$pay_log['id'],
    				         'out_trade_no' => $pay_log['out_trade_no'],
    				         'user_id' => $pay_log['user_id'],
    				         'refund_fee' => $pay_log['total_fee'],
    				         'addtime' => time(),
    				         'return_type' => 1
    				         );
    				$refund_id = M('refund_log')->insert($r_data);
    				$ref_res = order_WX_refund($refund_id);
    				if(!$ref_res){
    					//退款未成功，记录错误日志
    					$msg = "一元夺宝下单失败，退款至用户付款账户失败，订单编号：".$order['order_sn'].",用户ID:".$order['uid']."\r\n";
    					log_error($msg);
    				}
	        	}
	        	M('user_indiana')->where('id',$id)->save(array('order_status'=>7));//库存不足，订单过期			
    	    	$this->error('您未及时付款，目前库存不足，订单已作废',U('Mobile/User/indiana/'));
    	    }
 
    		//随机获取获奖号码
    		if($indiana['indiana_number']){
    		    $allnum = $ter = json_decode($indiana['indiana_number'],true);
    		    $existNum = M('user_indiana')->where("indiana_id",$order['indiana_id'])->field("indiana_number")->select();
    		    $eis = array();  		   
		   	    foreach($existNum as $key=>$val){
		   	    	if(!empty($val['indiana_number'])){
		   	    	    $t = json_decode($val['indiana_number'],true);
		   	  	        $eis = array_merge($eis,$t);	
		   	    	}
		   	    }
		   	    if(!empty($eis)){
		   	    	foreach($allnum as $key=>$val){
	    		   	    foreach($eis as $k=>$v){
		    		   	  	if($val == $v){
		    		   	  		unset($allnum[$key]);
		    		   	  	}
	    		   	    }
	    		    }
		   	    }

			    shuffle($allnum);
			    $get_number = array_slice($allnum,0,$order['num']);//抽取号码
			    $data['indiana_number'] = json_encode($get_number);
    		}
    		$r = M('user_indiana')->where('id',$id)->save($data);
    		$a = indiana_order_action('pay',$id,'付款');
    		if($r&$a){
    			$stock_num = $indiana['stock_num'] - $order['num'];//减编号库存
    		    M('indiana_sale')->where("id",$order['indiana_id'])->save(array('stock_num'=>$stock_num));
    			$this->success('付款成功！',U('Mobile/User/indiana',array('pay_status'=>1)));	
    		}
    		else{
    			$this->error('获取开奖码失败，请联系管理员',U('Mobile/User/indiana'));
    		}
    }


    /*
    * ajax 请求获取购物车列表
    */
    public function ajaxCartList()
    {
        $post_goods_num = I("goods_num/a"); // goods_num 购物车商品数量
        $post_cart_select = I("cart_select/a"); // 购物车选中状态
        $where['session_id'] = $this->session_id; // 默认按照 session_id 查询
        // 如果这个用户已经等了则按照用户id查询
        if($this->user_id){
            unset($where);
            $where['user_id'] = $this->user_id;
        }
        $cartList = M('Cart')->where($where)->getField("id,goods_num,selected,prom_type,prom_id");

        if($post_goods_num)
        {
            // 修改购物车数量 和勾选状态
            foreach($post_goods_num as $key => $val)
            {                
                $data['goods_num'] = $val < 1 ? 1 : $val;
                if($cartList[$key]['prom_type'] == 1) //限时抢购 不能超过购买数量
                {
                    $flash_sale = M('flash_sale')->where("id", $cartList[$key]['prom_id'])->find();
                    $data['goods_num'] = $data['goods_num'] > $flash_sale['buy_limit'] ? $flash_sale['buy_limit'] : $data['goods_num'];
                }

                $data['selected'] = $post_cart_select[$key] ? 1 : 0 ;
                if(($cartList[$key]['goods_num'] != $data['goods_num']) || ($cartList[$key]['selected'] != $data['selected']))
                    M('Cart')->where("id", $key)->save($data);
            }
            $this->assign('select_all', input('post.select_all')); // 全选框
        }
        $result = $this->cartLogic->cartList($this->user, $this->session_id,1,1);        
        if(empty($result['total_price']))
            $result['total_price'] = Array( 'total_fee' =>0, 'cut_fee' =>0, 'num' => 0, 'atotal_fee' =>0, 'acut_fee' =>0, 'anum' => 0);
        $this->assign('cellar_spec',C('cellar_spec'));//窖藏商品属性名称配置
        $this->assign('cartList', $result['cartList']); // 购物车的商品          
        $this->assign('total_price', $result['total_price']); // 总计
        return $this->fetch('ajax_cart_list');
    }

    /*
 * ajax 获取用户收货地址 用于购物车确认订单页面
 */
    public function ajaxAddress(){

        $regionList = M('Region')->getField('id,name');

        $address_list = M('UserAddress')->where("user_id", $this->user_id)->select();
        $c = M('UserAddress')->where("user_id = {$this->user_id} and is_default = 1")->count(); // 看看有没默认收货地址
        if((count($address_list) > 0) && ($c == 0)) // 如果没有设置默认收货地址, 则第一条设置为默认收货地址
            $address_list[0]['is_default'] = 1;

        $this->assign('regionList', $regionList);
        $this->assign('address_list', $address_list);
        return $this->fetch('ajax_address');
    }

    /**
     * ajax 删除购物车的商品
     */
    public function ajaxDelCart()
    {
        $ids = I("ids"); // 商品 ids
        $result = M("Cart")->where("id","in",$ids)->delete(); // 删除id为5的用户数据
        $return_arr = array('status'=>1,'msg'=>'删除成功','result'=>''); // 返回结果状态
        exit(json_encode($return_arr));
    }

}
